<?php

namespace Apps\Backend\Controllers;

use Phalcon\Mvc\View;
use Apps\Backend\Models\Vouchers;
use Apps\Backend\Models\Users;
use Apps\Backend\Forms\GroupsForm;
use Apps\Backend\Models\VegaArticle;
use Apps\Backend\Models\City;
use Apps\Backend\Models\District;
use Apps\Backend\Models\Ward;

class AdminUsersController extends \Phalcon\Mvc\Controller{
    public function initialize(){
        $this->tag->setTitle("VEGAFONE");
        $this->tag->setDefaults(array('keywords'=>'VEGAFONE',
            'description'=>'VEGAFONE',
            'backend_home'=>'active'));
        $this->view->users = $ss = $this->session->get('auth-identity');
        if(empty($ss)){
            $this->response->redirect(PUBLIC_URL_ADMIN);
        }
    }
    
    public function indexAction(){
        $data = Users::find(array("status = 1 AND group_admin = 0",'order'=>'id DESC'));
        
        $this->view->content = $data;
          
    }
    
    public function deleteAction($id=null){

        $dl = Users::findFirst("id = '{$id}'");
        $dl->delete();
        $this->response->redirect(PUBLIC_URL_ADMIN.'admin-users/index');
    }
    
    public function editAction($id=null){
        if($id ==1){
            $this->response->redirect(PUBLIC_URL_ADMIN.'login/noAccess');
        }
        $dl = Users::findFirst("id = '{$id}'");
        $this->view->v = $dl->toArray();
        if($this->request->isPost()){
            $data = $this->request->getPost();
            if(trim($data['password'])!=''){
                $passwprd = $this->seo->hash(trim($data['password']));
                $md5 = $data['password'];
            }else{
                $passwprd = $dl->password;
                $md5 = $dl->md5;
            }

            $dl->save(array('name'=>trim($data['name']),
                            'password'=>$passwprd,
                            'md5'=>$md5,
                            'address'=>$data['address'],
                            'phone'=>$data['phone'],
                            'modified'=>time(),
                            'status'=>$data['status'],
                    ));
            $this->response->redirect(PUBLIC_URL_ADMIN.'admin-users/index');
        }
    }
    
    public function addAction(){
        if($this->request->isPost()){
            $data = $this->request->getPost();
            $dl = new Users();
            $dl->name = $data['name'];
            $dl->username = strtolower($data['username']);
            $dl->password = $this->seo->hash($data['password']);
            $dl->md5 = $data['password'];
            $dl->created = time();
            $dl->status = $data['status'];
            $dl->address = $data['address'];
            $dl->phone = $data['phone'];
            $dl->ip = $this->request->getClientAddress();
            $dl->group_admin = 0;
            $dl->modified = time();
            $dl->visited_date = time();
            $dl->login_attempts = 0;
            $dl->save();
            $this->response->redirect(PUBLIC_URL_ADMIN.'admin-users/index');
        }
    }
}